package com.ly.controller;

import com.github.pagehelper.PageInfo;
import com.ly.bean.EasybuySeller;
import com.ly.bean.Result;
import com.ly.service.SellerService;
import com.ly.util.CookieUtil;
import org.apache.dubbo.config.annotation.Reference;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;

/**
 * @author 李岩
 * @date 2021/12/14-16:10
 * @describe
 */
@Controller
@RequestMapping("/seller/")
public class SellerController {
    private final Logger logger= LoggerFactory.getLogger(SellerController.class);

    @Reference(url = "dubbo://localhost:20880")
    private SellerService sellerService;

    private SellerController(){
        System.out.println("进入了买家的  controller层");
    }
    /**
     * 根据卖家名查询卖家对象
     */
    @GetMapping("login")
    @ResponseBody
    Result selectByPrimaryKey(EasybuySeller easybuySeller, HttpServletRequest request, HttpServletResponse response){
        System.out.println("卖家登录  "+easybuySeller);
        try {
            logger.info("这是卖家登录的controller层  "+easybuySeller.getSellerId());
            EasybuySeller seller = sellerService.selectByPrimaryKey(easybuySeller.getSellerId());
            if (seller==null){
                return Result.getResult(false,-1,"卖家用户名不存在",null);
            }else if (!seller.getPassword().equals(easybuySeller.getPassword())){
                return Result.getResult(false,-1,"卖家密码错误",seller);
            }
            request.getSession().setAttribute("seller",seller);
            CookieUtil.setCookie(request,response,"sellerId",seller.getSellerId());
            return Result.getResult(true,200,"登录成功",seller);
        } catch (Exception e) {
            logger.info("这是卖家登录的controller层 出现了异常"+e);
        }
        return Result.getResult(false,-1,"登录失败",null);
    }

    @RequestMapping("selectAllSeller")
    @ResponseBody
    Result selectAllSeller(String pageNum,String pageSize){
        System.out.println("展示所有卖家  pageNum="+pageNum+"   pageSize="+pageSize);
        try {
            logger.info("这是展示所有卖家的controller层");
            PageInfo<EasybuySeller> pageInfo = sellerService.selectAllSeller(pageNum,pageSize);
            HashMap<String, Object> hashMap = new HashMap<>();
            hashMap.put("sellerList",pageInfo.getList());
            hashMap.put("pageNum",pageInfo.getPageNum());
            hashMap.put("total",pageInfo.getTotal());
            Result result = Result.getResult(true,200,"查询成功",hashMap);
            System.out.println(result);
            return result;
        } catch (Exception e) {
            logger.info("这是展示所有卖家的controller层 出现了异常"+e);
        }
        Result result = Result.getResult(false,4,"查询失败",null);
        System.out.println(result);
        return result;
    }
}
